package sorter;

import java.lang.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Random;

public class SortClient {

	private ArrayList<Comparable> resultArray = new ArrayList<Comparable>();

	public SortClient() throws RemoteException {

	}

	public static void main(String[] args) throws IOException, RemoteException {

		try {
			SortClient mySortClient = new SortClient();
			mySortClient.sort();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void sort() throws Exception {
		try {
			Registry registry = LocateRegistry.getRegistry(1100);
			ISortFactory myISortFactory = (ISortFactory) registry
					.lookup("mysortfactory");
			Comparable[] list = generateRandomList(100, Integer.class);
			Comparable[][] lists = spliceList(list, 1);
			ListJoiner myListJoiner = new ListJoiner(1);
			//System.out.println("" + System.nanoTime());
			for (Comparable[] lst : lists) {				
				SorterThread mySorterThread = new SorterThread(lst,myListJoiner);
				Thread mythread = new Thread(mySorterThread);
				mythread.setDaemon(false);
				mythread.start();	
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}

	private Comparable[][] spliceList(Comparable[] list, int aantalSorteerders)
			throws Exception {
		int max = 1000;
		int verschil = max % aantalSorteerders;
		if (aantalSorteerders > 0) {
			int deel = max / aantalSorteerders;
			Comparable[][] lijst = new Comparable[aantalSorteerders][];
			
			for (int s = 0; s < aantalSorteerders; s++) {
				ArrayList lijstje = new ArrayList();
				for (int i = 0; i < list.length; i++) {
					if (verschil == 0){
					if ((Integer) list[i] > s * deel
							&& (Integer) list[i] < s * deel + deel) {
						lijstje.add(list[i]);
					}
					}
					else
					{
						if ((Integer) list[i] > s * deel
								&& (Integer) list[i] < s * deel + deel && s != aantalSorteerders-1) {
							lijstje.add(list[i]);
						}
						if ((Integer) list[i] > s * deel
								&& (Integer) list[i] < s * deel + deel+verschil && s == aantalSorteerders-1) {
							lijstje.add(list[i]);
						}
					}
					lijst[s] = (Comparable[]) lijstje
							.toArray(new Comparable[0]);
				}
			}
			return lijst;
		} else {
			throw new Exception();
		}

	}

	private Comparable[] generateRandomIntegers(int length) {
		Comparable[] randomArray = new Comparable[length];
		Random randomGenerator = new Random();
		for (int i = 0; i < length; i++) {
			Comparable randomInt = (Integer) randomGenerator.nextInt(1000);
			randomArray[i] = randomInt;
		}
		return randomArray;
	}

	private Comparable[] generateRandomStrings(int length) {
		Comparable[] randomArray = new Comparable[length];
		Random randomGenerator = new Random();
		for (int i = 0; i <= length; i++) {
			Comparable randomChar = "" + (char) randomGenerator.nextInt(255);
			randomArray[i] = randomChar;
		}
		return randomArray;
	}

	private Comparable[] generateRandomList(int length, Class type) {
		Comparable[] newArray;
		newArray = new Comparable[length];
		if (type == String.class) {
			newArray = generateRandomStrings(length);
		}
		if (type == Movie.class) {
			newArray = generateRandomMovies(length);
		}
		if (type == Integer.class) {
			newArray = generateRandomIntegers(length);
		}
		else {
			newArray = generateRandomIntegers(length);
		}			
		return newArray;
	}

	private Comparable[] generateRandomMovies(int length) {
		Comparable[] randomArray = new Movie[length];
		Random randomGenerator = new Random();
		for (int i = 0; i <= length; i++) {
			Comparable randomMovies = (char) randomGenerator.nextInt(255);
			randomArray[i] = new Movie(generateString(12),
					generateString(10), randomGenerator.nextInt(2012));
		}
		return randomArray;
	}

	public String generateString(int lengte) {
		Random randomGenerator = new Random();
		Comparable randomChar = "";
		for (int i = 0; i < lengte; i++) {
			randomChar = "" + randomChar + (char) randomGenerator.nextInt(255);
		}
		return (String) randomChar;
	}
}
